package com.tjetc.service.impl;

import com.tjetc.dao.DepartmentDao;
import com.tjetc.dao.NurseDao;
import com.tjetc.dao.impl.DepartmentDaoImpl;
import com.tjetc.dao.impl.NurseDaoImpl;
import com.tjetc.domain.Department;
import com.tjetc.domain.Nurse;
import com.tjetc.service.NurseService;
import com.tjetc.utils.Db;
import com.tjetc.utils.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class NurseServiceImpl implements NurseService {
    private NurseDao nurseDao=new NurseDaoImpl();
    private DepartmentDao departmentDao=new DepartmentDaoImpl();

    @Override
    public Nurse login(String username, String password) {
        try {
            return nurseDao.login(username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getCount(String name) {
        try {
            return nurseDao.getCount(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Nurse findByName(String username) {
        try {
            return nurseDao.findByName(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Nurse> list(String name, PageInfo<Nurse> pageInfo) {
        List<Nurse> list= null;
        try {
            list = nurseDao.list(name,pageInfo);
            for (Nurse nurse : list) {
                Department department=departmentDao.getById(nurse.getDepid());
                nurse.setDepartment(department);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Nurse> queryByDepid(int depid) {
        try {
            return nurseDao.queryByDepid(depid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Nurse nurse) {
        try {
            //开启事务
            Db.startTransaction();
            //将nurse存入护士表
            nurseDao.add(nurse);
            //将护士所属的小科室人数加1
            Department department=departmentDao.getById(nurse.getDepid());
            department.setStaffs(department.getStaffs()+1);
            departmentDao.addNum(department);

            //将护士所属的大科室人数加1
            Department department1=departmentDao.getById(department.getParent_id());
            department1.setStaffs(department1.getStaffs()+1);
            departmentDao.addNum(department1);
            //提交事务
            Db.commit();
            return  true;

        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            Db.rollback();
        } finally {
            //关闭连接
            Db.close();
        }
        return false;
    }

    @Override
    public Nurse getById(int id) {
        try {
            return nurseDao.getById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean update(Nurse nurse) {
        try {
            return nurseDao.update(nurse)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean del(int id) {
        try {
            Db.startTransaction();
            //删除护士表
            nurseDao.del(id);
            //将护士所属的小科室人数减1
            Nurse nurse=nurseDao.getById(id);
            Department department=departmentDao.getById(nurse.getDepid());
            department.setStaffs(department.getStaffs()-1);
            departmentDao.addNum(department);

            //将护士所属的大科室人数减1
            Department department1=departmentDao.getById(department.getParent_id());
            department1.setStaffs(department1.getStaffs()-1);
            departmentDao.addNum(department1);
            //提交事务
            Db.commit();
            return  true;

        } catch (Exception e) {
            e.printStackTrace();
            Db.rollback();
        } finally {
            Db.close();
        }
        return false;
    }

    @Override
    public Nurse getByName(String username) {
        try {
            return nurseDao.getByName(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Nurse getpwdById(int id) {
        try {
            return nurseDao.getpwdById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean updatepwd(Nurse nurse) {
        try {
            return nurseDao.updatepwd(nurse)>0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
